clear; clc;
path='D:\File\B_TFD\数据集2\I-B-1';
load(path);

threshold=4;                       %设置阈值用于调整波形
fs=200000;
data_len=length(Channel_2);

Channel_2(Channel_2>threshold)=5; %将大于阈值的数置为一样的，方便后面查找
Channel_2(Channel_2<threshold)=0;

count_interval=2000;              %以1000个点为一个小区间，算平均速度

speed=zeros(data_len/count_interval,1);    %初始化储存速度的矩阵
j=0;

for i=1:data_len/count_interval
    count=0;
    template=Channel_2(j+1:j+count_interval,1);%临时变量将数据取出来
    j=j+count_interval;
    for k=1:count_interval-1
        if(template(k,1)<template(k+1,1))  %如果遇到上升沿，就计数
            count=count+1;
        end
    end
    speed(i,1)=count*(fs/count_interval)/1024;   %计算每秒转速，按转一圈发1024个脉冲算
end

t=1/fs:1*count_interval/fs:data_len/fs;
t=t';
plot(t,speed);
xlim([0 6.5536])
title('rotational frequency')